package library.and.xps.com.step.util;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import org.json.JSONException;

import library.and.xps.com.step.App;


public class RequestLocation {
    private static final RequestLocation ourInstance = new RequestLocation();
    private LocationClient mLocationClient;
    private onLocalResponse onLocalResponse;

    public static RequestLocation getInstance() {
        return ourInstance;
    }

    private RequestLocation() {
    }

    public void setRequestLocal(onLocalResponse onLocalResponse) {
        mLocationClient = new LocationClient(App.getInstance().getApplicationContext());
        this.onLocalResponse = onLocalResponse;
    }

    public void getLocation() {
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {

                if (bdLocation != null) {
                    if (bdLocation.getCity() != null
                            && !"".equals(bdLocation.getCity())) {
                        try {
                            onLocalResponse.onSuccess(bdLocation);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    onLocalResponse.onFaild("失败");
                }
            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {

            }
        });

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setScanSpan(0);
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    public interface onLocalResponse {
        void onSuccess(BDLocation bdLocation) throws JSONException;

        void onFaild(String faild);
    }
}
